package controller

import (
	"gitee.com/molonglove/goboot/gin"
	"helper-api/app/constant"
	"helper-api/app/models/request"
	"helper-api/app/models/response"
	"helper-api/app/service"
	"helper-api/utils"
	"net/http"
	"strconv"
)

var Script = new(ScriptController)

type ScriptController struct{}

// Tree 服务树
func (s *ScriptController) Tree(c *gin.Context) {
	var (
		param     request.ScriptTreeRequest
		tree      []response.ScriptTreeResponse
		customErr *response.BusinessError
		err       error
	)
	c.DefaultPostForm("expendRoot", "false")
	c.DefaultPostForm("isTree", "true")
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail("请求参数错误"))
		return
	}
	if tree, customErr = service.Server.Tree(&param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(tree))
}

// GroupList 分组列表
func (s *ScriptController) GroupList(c *gin.Context) {
	result, err := service.Server.GroupList()
	if err != nil {
		c.JSON(http.StatusOK, response.ResultCustom(err))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// GroupCreate 分组创建
func (s *ScriptController) GroupCreate(c *gin.Context) {
	var (
		result    *response.ScriptTreeResponse
		param     request.ServerGroupCreateRequest
		customErr *response.BusinessError
		err       error
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	param.UserId = c.GetInt64(constant.HeaderUserKey)
	if result, customErr = service.Server.GroupCreate(&param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// GroupUpdate 分组更新
func (s *ScriptController) GroupUpdate(c *gin.Context) {
	var (
		result    *response.ScriptTreeResponse
		param     request.ScriptGroupUpdateRequest
		customErr *response.BusinessError
		err       error
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	param.UserId = c.GetInt64(constant.HeaderUserKey)
	if result, customErr = service.Server.GroupUpdate(&param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// GroupDelete 分组删除
func (s *ScriptController) GroupDelete(c *gin.Context) {
	var (
		userId    int64
		query     string
		ids       []int64
		customErr *response.BusinessError
	)
	if query = c.DefaultQuery("ids", ""); query == "" {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	if ids = utils.StrToArray(query); len(ids) <= 0 {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	userId = c.GetInt64("userId")
	if customErr = service.Server.GroupDelete(ids, userId); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok("删除成功"))

}

// ================================= server =================================================

// ServerCreate 服务创建
func (s *ScriptController) ServerCreate(c *gin.Context) {
	var (
		result    *response.ScriptTreeResponse
		param     request.ScriptItemCreateRequest
		customErr *response.BusinessError
		err       error
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail("请求参数错误"))
		return
	}
	param.UserId = c.GetInt64(constant.HeaderUserKey)
	if result, customErr = service.Server.ServerCreate(&param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// ServerUpdate 更新服务
func (s *ScriptController) ServerUpdate(c *gin.Context) {
	var (
		result    *response.ScriptTreeResponse
		param     request.ServerItemUpdateRequest
		customErr *response.BusinessError
		err       error
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	param.UserId = c.GetInt64(constant.HeaderUserKey)
	if result, customErr = service.Server.ServerUpdate(&param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// ServerList 获取服务列表
func (s *ScriptController) ServerList(c *gin.Context) {
	var (
		query     string
		gId       int64
		customErr *response.BusinessError
		result    []response.ScriptItemListResponse
		err       error
	)
	if query = c.DefaultQuery("gId", ""); query == "" {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	if gId, err = strconv.ParseInt(query, 10, 64); err != nil {
		c.JSON(http.StatusOK, response.Fail(response.RequestParamError))
		return
	}
	result, customErr = service.Server.ServerList(gId)
	if err != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// ================================= script =================================================

// ScriptList 获取服务列表
func (s *ScriptController) ScriptList(c *gin.Context) {
	var (
		query     string
		sId       uint64
		customErr *response.BusinessError
		result    []response.ServerScriptListResponse
		err       error
	)
	if query = c.DefaultQuery("sId", ""); query == "" {
		c.JSON(http.StatusOK, response.Fail("请求参数错误"))
		return
	}
	if sId, err = strconv.ParseUint(query, 10, 64); err != nil {
		c.JSON(http.StatusOK, response.Fail("请求参数格式不正确"))
		return
	}
	result, customErr = service.Server.ScriptList(sId)
	if err != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// ScriptCreate 脚本创建
func (s *ScriptController) ScriptCreate(c *gin.Context) {
	var (
		result    *response.ServerScriptListResponse
		param     request.ServerScriptCreateRequest
		customErr *response.BusinessError
		err       error
	)
	if err = c.ShouldBindJSON(&param); err != nil {
		c.JSON(http.StatusOK, response.Fail("请求参数错误"))
		return
	}
	param.UserId = c.GetInt64(constant.HeaderUserKey)
	if result, customErr = service.Server.ScriptCreate(&param); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok(result))
}

// ScriptExecute 脚本执行
func (s *ScriptController) ScriptExecute(c *gin.Context) {
	var (
		id        uint64
		userId    uint64
		query     string
		customErr *response.BusinessError
	)
	if query = c.DefaultQuery("id", ""); query == "" {
		c.JSON(http.StatusOK, response.Fail("请求参数错误"))
		return
	}
	id, _ = strconv.ParseUint(query, 10, 64)
	userId = c.GetUint64(constant.HeaderUserKey)
	if customErr = service.Server.ScriptExecute(id, userId); customErr != nil {
		c.JSON(http.StatusOK, response.ResultCustom(customErr))
		return
	}
	c.JSON(http.StatusOK, response.Ok("开始执行"))
}
